package com.app.shortvideo.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.app.shortvideo.R
import com.app.library.ext.visible
import com.blankj.utilcode.util.SizeUtils

class ArrowItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var ivIcon: ImageView? = null
    private var ivArrow: ImageView? = null
    private var tvDesc: TextView? = null
    private var tvValue: TextView? = null


    init {
        initComponents(context, attrs, defStyleAttr)
    }

    private fun initComponents(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        val typeArray =
            context.obtainStyledAttributes(
                attrs,
                R.styleable.ArrowItemView,
                defStyleAttr,
                defStyleAttr
            )
        val desc = typeArray.getString(R.styleable.ArrowItemView_descText)
        val value = typeArray.getString(R.styleable.ArrowItemView_valueText)
        val valueHint = typeArray.getString(R.styleable.ArrowItemView_valueHint)
        val color = typeArray.getColor(
            R.styleable.ArrowItemView_valueColor,
            resources.getColor(R.color.color_text_1)
        )
        val showValue = typeArray.getBoolean(R.styleable.ArrowItemView_showValue, false)
        val showArrow = typeArray.getBoolean(R.styleable.ArrowItemView_showArrow, true)
        val icon = typeArray.getResourceId(R.styleable.ArrowItemView_icon, 0)
        typeArray.recycle()

        setPadding(SizeUtils.dp2px(8f), 0, SizeUtils.dp2px(8f), 0)
        inflate(context, R.layout.view_arrow_item, this)
        orientation = HORIZONTAL
        ivIcon = findViewById(R.id.ivItemIcon)
        ivArrow = findViewById(R.id.ivItemArrow)
        tvDesc = findViewById(R.id.tvItemDesc)
        tvValue = findViewById(R.id.tvItemValue)

        if (desc.isNullOrEmpty()) {
            tvDesc?.text = ""
        } else {
            tvDesc?.text = desc
        }
        if (icon == 0) {
            ivIcon?.visible(false)
        } else {
            ivIcon?.visible(true)
            ivIcon?.setImageResource(icon)
        }
        if (showValue) {
            tvValue?.visible(true)
            tvValue?.setTextColor(color)
            valueHint?.let { tvValue?.hint = it }
            if (value != null) {
                tvValue?.text = value
            }
        }

        ivArrow?.visible(showArrow)
    }

    fun setValue(value: String) {
        tvValue?.visible(true)
        tvValue?.text = value
    }

}